package com.gitee.dynamicproxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author ming.yan-1
 * @version V1.0
 * @Title com.gitee.dynamicproxy.jdk
 * @Description
 * @date 2018/9/11  下午6:28
 */
public class JdkDemo implements InvocationHandler{

    private Object object;

    public Object instance(Object o) {
        this.object = object;
        return Proxy.newProxyInstance(o.getClass().getClassLoader(),o.getClass().getInterfaces(),this);
    }

    /**
     *
     * @param proxy  代理类的实例
     * @param method 反射得到的类的方法
     * @param args 参数
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("执行之前增强下");
        Object result = null;
        try {
            //此处传入的必须是代理的object，如果是proxy会递归调用
            result = method.invoke(object,args);
        } catch (IllegalAccessException e) {
        } catch (IllegalArgumentException e) {
        } catch (InvocationTargetException e) {
        }
        System.out.println("执行之后增强下");
        return result;
    }
}
